package com.wsh.generator.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsh.entity.generator.GenBaseConfEntity;
import com.wsh.entity.generator.GenConfEntity;
import com.wsh.entity.generator.GenFileConfEntity;
import com.wsh.entity.generator.TableColumnInfoEntity;
import com.wsh.generator.GenBaseConfService;
import com.wsh.generator.GenConfService;
import com.wsh.generator.GenFileConfService;
import com.wsh.generator.GenerCodeService;
import com.wsh.mapper.generator.GenConfDao;
import com.wsh.mapper.generator.TableColumnInfoDao;
import com.wsh.utils.FileUtil;
import com.wsh.utils.generator.GenUtils;
import com.wsh.vo.generator.TablesConfVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GenConfServiceImpl extends ServiceImpl<GenConfDao, GenConfEntity> implements GenConfService {
    @Autowired
    private GenerCodeService generCodeService;

    @Autowired
    private GenBaseConfService genBaseConfService;

    @Autowired
    private GenFileConfService genFileConfService;

    @Autowired
    private TableColumnInfoDao tableColumnInfoDao;

    @Override
    public Object getConfList(String tableName) {
        List<GenConfEntity> genConfEntities = getGenConfList(tableName);
        if (genConfEntities == null || genConfEntities.size() == 0) {
            return generCodeService.queryColumns(tableName);
        }
        return genConfEntities;
    }

    @Override
    public String genCode(String tableName) throws Exception {
        List<GenConfEntity> genConfList = getGenConfList(tableName);
        GenBaseConfEntity baseConf = genBaseConfService.getById(tableName);
        List<GenFileConfEntity> genFileConfEntities = genFileConfService.getByTableName(tableName);
        if ((genConfList == null && genConfList.size() == 0) || baseConf == null) {
            throw new Exception("请配置表单或基本配置信息");
        }
        String zipName = GenUtils.genCode(genConfList, baseConf, genFileConfEntities);
        return FileUtil.zipFile(zipName, baseConf.getTableName(), true);
    }

    @Override
    public void saveConf(TablesConfVo tablesConfVo) {
        String tableName = tablesConfVo.getTableName();

        Map<String, GenConfEntity> tableData = tablesConfVo.getTableData().get("tableData");
        tableData.entrySet().iterator().forEachRemaining(item -> {
            String key = item.getKey();
            GenConfEntity genConfEntity = item.getValue();
            if (!genConfEntity.getFormType().equals("file")) {
                //
                TableColumnInfoEntity entity = new TableColumnInfoEntity();
                entity.setTableName(tableName);
                entity.setOName(key);
                String type = tableColumnInfoDao.queryColumnType(entity);
                int length = Integer.parseInt(tableColumnInfoDao.queryColumnLength(entity));

                //
                genConfEntity.setColType(type);
                Integer maxLength = genConfEntity.getMaxLength();
                if (maxLength > length || maxLength == 0) {
                    maxLength = length;
                }

                genConfEntity.setMaxLength(maxLength);
            }


            saveOrUpdate(genConfEntity);
        });
    }

    public List<GenConfEntity> getGenConfList(String tableName) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("table_name", tableName);
        return baseMapper.selectByMap(maps);
    }
}
